package org.tmsps.ne4spring.utils.http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpFileTools {

	/**
	 * 
	 * @param file
	 * @return
	 */
	@Deprecated
	public static String uploadFile(String url, File file) {
		String strPostResponseBody = "";
		HttpClient httpclient = new DefaultHttpClient();

		try {
			HttpPost httppost = new HttpPost(url);

			FileBody bin1 = new FileBody(file);

			// StringBody comment = new StringBody(filename1);

			MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
					Charset.forName("UTF-8"));

			reqEntity.addPart("file", bin1);// file1为请求后台的File upload;属性
			// reqEntity.addPart("filename1", comment);// filename1为请求后台的普通参数;属性
			httppost.setEntity(reqEntity);

			HttpResponse response = httpclient.execute(httppost);

			int statusCode = response.getStatusLine().getStatusCode();
			System.err.println(statusCode);
			if (statusCode == HttpStatus.SC_OK) {

				System.err.println("服务器正常响应.....");

				HttpEntity resEntity = response.getEntity();

				strPostResponseBody = EntityUtils.toString(resEntity);

				EntityUtils.consume(resEntity);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.getConnectionManager().shutdown();
			} catch (Exception ignore) {
			}
		}
		return strPostResponseBody;

	}

	public synchronized static boolean downLoad(String url, String localPath, boolean widthProcessBar) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		OutputStream out = null;
		InputStream in = null;

		try {
			HttpGet httpGet = new HttpGet(url);

			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			in = entity.getContent();

			long length = entity.getContentLength();
			if (length <= 0) {
				System.out.println("下载文件不存在！");
				return false;
			}
			System.err.println("length:" + length);
			System.out.println("The response value of token:" + httpResponse.getFirstHeader("token"));

			File file = new File(localPath);
			if (!file.exists()) {
				file.createNewFile();
			}

			out = new FileOutputStream(file);
			byte[] buffer = new byte[4096];
			int readLength = 0;
			if (widthProcessBar) {
				int p = 0;
				while ((readLength = in.read(buffer)) > 0) {
					byte[] bytes = new byte[readLength];
					System.arraycopy(buffer, 0, bytes, 0, readLength);
					out.write(bytes);
					p += readLength;
					BigDecimal d = new BigDecimal(p).divide(new BigDecimal(length), 2, BigDecimal.ROUND_HALF_UP);
					// BrowserWindow.bar.setValue(d.multiply(new
					// BigDecimal(100)).intValue());
				}
			} else {
				while ((readLength = in.read(buffer)) > 0) {
					byte[] bytes = new byte[readLength];
					System.arraycopy(buffer, 0, bytes, 0, readLength);
					out.write(bytes);
				}
			}

			out.flush();

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public synchronized static boolean downLoad(String url, String localPath) {
		return downLoad(url, localPath, false);
	}

	public static void main(String[] args) {
		File file = new File("c:\\test3.docx");
		String url = "http://doc.eqbidding.com/upload_docx_new.htm";
		String body = HttpFileTools.uploadFile(url, file);
		System.err.println(body);

	}

}
